<?php

class colorboxJs extends JsPlugin {

    static private $instance;
    public static function getInstanceOf($plugin){
        $class_name = __CLASS__;
        if (!isset(self::$instance)) self::$instance = new $class_name($plugin);
        return self::$instance;
    }
    
    public function init(){
        $this->Html->LoadJs($this->url.'/js/jquery.colorbox');
        $this->Html->loadCss('plugins/jqueryui/colorbox');
        $this->Html->LoadJsFunction("
            function Resize_Box(){
                var x = $('iframe .formulario').width();
                var y = $('iframe .formulario').height();
                parent.$.fn.colorbox.resize({
                    innerWidth: x, innerHeight: y
                    //width: x, height: y
                });
            }
        ");
            
    }
    
    public function formDialog($selector, $model){
        static $selector_list = array();
        
        //evita entrada de formulários duplicados
        if(in_array($selector, $selector_list)) return;
        
        $this->LoadResource('formulario', 'form');
        $this->form = new formularioResource();
        $this->form->printable();
        $html = $this->form->execute($model, array(), array(), "$model/formulario/ajax");
        
        echo "<div style='display:none'>$html</div>";
        $this->Html->LoadJsFunction("
            $('$selector').colorbox({inline:true, minWidth: '350px',
        minHeight: '250px',});
        ");
        $selector_list[] = $selector;
    }
    
    public function ajaxDialog($seletor){
        $this->Html->LoadJsFunction("
            $('$seletor').colorbox({
                innerWidth: 430, innerHeight: 370,
                onComplete: function(){
                    Resize_Box();
                }
            });
        ");
    } 

}

?>